package ija.view.elements;

import ija.controller.ReplayerController.ReplayState;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Image;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;
import javax.swing.BorderFactory;
import javax.swing.GroupLayout;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SwingConstants;
import javax.swing.border.Border;

public class ReplayerButtons extends JPanel {

    private static final long serialVersionUID = -8121750505175710555L;

    private JButton bFirst, bLast, bPrevious, bNext, bPause, bStop, bPlay;
    private JTextField tfTime;
    private JLabel lTime;
    private JTextField tfStopAt;
    private JLabel lStopAt;
    private GroupLayout layout;
    private ReplayState replayState;
    protected Image iPlay = null;

    // region Constructors
    public ReplayerButtons(String name) {
        super(new BorderLayout());
        Border blackline = BorderFactory.createLineBorder(Color.lightGray);
        try {
            this.iPlay = ImageIO.read(new File("graphic/play.png")).getScaledInstance(100, 45, 1);
        } catch (IOException e) {
            System.err.println("File not found: " + e.getMessage());
        }
        this.setBorder(BorderFactory.createTitledBorder(blackline, name));
        this.addWidgets();
    }

    // endregion Constructors
    // region Getters
    public ReplayState getState() {
        return this.replayState;
    }

    public String getTime() {
        return this.tfTime.getText();
    }

    public String getStopAt() {
        return this.tfStopAt.getText();
    }

    // endregion Getters
    // region Setters
    public void setState(ReplayState rs) {
        this.replayState = rs;
        switch (rs) {
        case PLAYING:
            this.bFirst.setEnabled(false);
            this.bPrevious.setEnabled(false);
            this.bPlay.setEnabled(false);
            this.bPause.setEnabled(true);
            this.bStop.setEnabled(true);
            this.bNext.setEnabled(false);
            this.bLast.setEnabled(false);
            this.tfTime.setEnabled(false);
            this.tfStopAt.setEnabled(false);
            break;
        case PAUSED:
            this.bFirst.setEnabled(true);
            this.bPrevious.setEnabled(true);
            this.bPlay.setEnabled(true);
            this.bPause.setEnabled(false);
            this.bStop.setEnabled(true);
            this.bNext.setEnabled(true);
            this.bLast.setEnabled(true);
            this.tfTime.setEnabled(true);
            this.tfStopAt.setEnabled(false);
            break;
        case STOPPED:
            this.bFirst.setEnabled(false);
            this.bPrevious.setEnabled(false);
            this.bPlay.setEnabled(true);
            this.bPause.setEnabled(false);
            this.bStop.setEnabled(false);
            this.bNext.setEnabled(true);
            this.bLast.setEnabled(true);
            this.tfTime.setEnabled(true);
            this.tfStopAt.setEnabled(true);
            break;
        }
    }

    public void setTime(String s) {
        this.tfTime.setText(s);
    }

    public void setStopAt(String s) {
        this.tfStopAt.setText(s);
    }

    // endregion Setters
    // region Listeners
    public void addPlayListener(ActionListener play) {
        this.bPlay.addActionListener(play);
    }

    public void addPauseListener(ActionListener pause) {
        this.bPause.addActionListener(pause);
    }

    public void addStopListener(ActionListener stop) {
        this.bStop.addActionListener(stop);
    }

    public void addPreviousListener(ActionListener previous) {
        this.bPrevious.addActionListener(previous);
    }

    public void addNextListener(ActionListener next) {
        this.bNext.addActionListener(next);
    }

    public void addFirstListener(ActionListener first) {
        this.bFirst.addActionListener(first);
    }

    public void addLastListener(ActionListener last) {
        this.bLast.addActionListener(last);
    }

    // endregion Listeners
    private void addWidgets() {
        Dimension d = new Dimension(50, 20);
        Font f = new Font("Dialog", Font.PLAIN, 10);

        this.setPreferredSize(new Dimension(650, 70));
        this.setFont(f);

        this.bFirst = new JButton("<<");
        this.bFirst.setPreferredSize(d);
        this.bFirst.setMinimumSize(d);
        this.bFirst.setMaximumSize(d);
        this.bFirst.setFont(f);

        this.bLast = new JButton(">>");
        this.bLast.setPreferredSize(d);
        this.bLast.setMinimumSize(d);
        this.bLast.setMaximumSize(d);
        this.bLast.setFont(f);

        this.bPrevious = new JButton("<");
        this.bPrevious.setPreferredSize(d);
        this.bPrevious.setMinimumSize(d);
        this.bPrevious.setMaximumSize(d);
        this.bPrevious.setFont(f);

        this.bNext = new JButton(">");
        this.bNext.setPreferredSize(d);
        this.bNext.setMinimumSize(d);
        this.bNext.setMaximumSize(d);
        this.bNext.setFont(f);

        this.bPlay = new JButton("|>");
        this.bPlay.setPreferredSize(d);
        this.bPlay.setMinimumSize(d);
        this.bPlay.setMaximumSize(d);
        this.bPlay.setFont(f);
        // this.bPlay.setIcon(new ImageIcon(this.iPlay));

        this.bStop = new JButton("|_|");
        this.bStop.setPreferredSize(d);
        this.bStop.setMinimumSize(d);
        this.bStop.setMaximumSize(d);
        this.bStop.setFont(f);

        this.bPause = new JButton("||");
        this.bPause.setPreferredSize(d);
        this.bPause.setMinimumSize(d);
        this.bPause.setMaximumSize(d);
        this.bPause.setFont(f);

        this.lTime = new JLabel("Interval[s]:", SwingConstants.LEFT);
        this.tfTime = new JTextField("3", 4);
        this.tfTime.setMaximumSize(d);
        this.tfTime.setMinimumSize(d);
        this.tfTime.setPreferredSize(d);
        this.tfTime.setFont(f);

        this.lStopAt = new JLabel("Stop At:", SwingConstants.LEFT);
        this.tfStopAt = new JTextField(4);
        this.tfStopAt.setMaximumSize(d);
        this.tfStopAt.setMinimumSize(d);
        this.tfStopAt.setPreferredSize(d);
        this.tfStopAt.setFont(f);

        this.layout = new GroupLayout(this);
        this.setLayout(this.layout);
        this.layout.setAutoCreateGaps(true);
        this.layout.setAutoCreateContainerGaps(true);

        this.layout.setHorizontalGroup(this.layout
                .createSequentialGroup()
                .addComponent(this.bFirst, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE,
                        GroupLayout.DEFAULT_SIZE)
                .addComponent(this.bPrevious, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE,
                        GroupLayout.DEFAULT_SIZE)
                .addComponent(this.bPlay, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE,
                        GroupLayout.DEFAULT_SIZE)
                .addComponent(this.bPause, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE,
                        GroupLayout.DEFAULT_SIZE)
                .addComponent(this.bStop, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE,
                        GroupLayout.DEFAULT_SIZE)
                .addComponent(this.bNext, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE,
                        GroupLayout.DEFAULT_SIZE)
                .addComponent(this.bLast, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE,
                        GroupLayout.DEFAULT_SIZE)
                .addComponent(this.lTime, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE,
                        GroupLayout.DEFAULT_SIZE)
                .addComponent(this.tfTime, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE,
                        GroupLayout.DEFAULT_SIZE)
                .addComponent(this.lStopAt, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE,
                        GroupLayout.DEFAULT_SIZE)
                .addComponent(this.tfStopAt, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE,
                        GroupLayout.DEFAULT_SIZE));

        this.layout.setVerticalGroup(this.layout
                .createParallelGroup(GroupLayout.Alignment.LEADING, false)
                .addComponent(this.bFirst, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE,
                        Short.MAX_VALUE)
                .addComponent(this.bPrevious, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE,
                        Short.MAX_VALUE)
                .addComponent(this.bPlay, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE,
                        Short.MAX_VALUE)
                .addComponent(this.bPause, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE,
                        Short.MAX_VALUE)
                .addComponent(this.bStop, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE,
                        Short.MAX_VALUE)
                .addComponent(this.bNext, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE,
                        Short.MAX_VALUE)
                .addComponent(this.bLast, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE,
                        Short.MAX_VALUE)
                .addComponent(this.lTime, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE,
                        Short.MAX_VALUE)
                .addComponent(this.tfTime, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE,
                        Short.MAX_VALUE)
                .addComponent(this.lStopAt, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE,
                        GroupLayout.DEFAULT_SIZE)
                .addComponent(this.tfStopAt, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE,
                        GroupLayout.DEFAULT_SIZE));
    }
}